$(function () {
    let isAccount=false,
        isPassword=false;
    let $account=$('input[name=telephone]');
    $account.blur(fnCheckAccount);
    function fnCheckAccount() {
        isAccount=false;
        let sAccount=$account.val();
        if(sAccount===''){
            message.showError("账号不能为空");
            return
        }
        else{
            isAccount=true;
            return
        }
    }
    let $password=$('input[name=password]');
    $password.blur(fnCheckPassword);
    function fnCheckPassword() {
        isPassword=false;
        let sPassword=$password.val();
        if(sPassword===''){
            message.showError("密码不能为空");
        }else{
            isPassword=true;
            return
        }
    }

    let btn=$('.login-btn');
    btn.click(function (e) {
        e.preventDefault();
        if(!isAccount){
            fnCheckAccount();
            return
        }
        if(!isPassword){
            fnCheckPassword();
            return
        }
        let remember=$('input[name=remember]').val();
        $.ajax({
            url:"/user/login/",
            type:"POST",
            dataType:"json",
            data:{
                account:$account.val(),
                password:$password.val(),
                remember:remember
            },
            success:function (res) {
               if(res.errno==='0'){
                   message.showSuccess(res.errmsg);
                   setTimeout(function () {
                       window.location.href='/'
                   },1000)
               }else {
                   message.showError(res.errmsg);
                   return
               }
            },
            error(xhr,msg){
                message.showError("服务器超时,请重试");
                return
            }
        })
    })
});